﻿using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.IO;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class AC_ActivityDtls : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        IJobSiteBLL jBLL = new JobSiteBLL();
        IEmployeeBLL EmpBLL = new EmployeeBLL();
        IEmployeeTypeBLL etBLL = new EmployeeTypeBLL();
        ActivityBLL ActivityBLL = new ActivityBLL();
        EssMessageBLL mesBLL = new EssMessageBLL();
        ActivityRequestBLL ActReqBLL = new ActivityRequestBLL();

        private const int ItemsPerRequest = 10;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListEmployeeTypes();
                LoadDetail();
                GetMessageApproved();
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        private void ListEmployeeTypes()
        {
            IList<EmployeeTypeInfo> infoList = etBLL.ListEmployeeTypes();
            cmbEmployeeType.DataSource = infoList;
            cmbEmployeeType.DataBind();

            cmbEmployeeTypeToolTip.DataSource = infoList;
            cmbEmployeeTypeToolTip.DataBind();
        }

        protected void LoadDetail()
        {
            int Sequence = Int32.Parse(Request.QueryString["Sequence"].ToString());
            DataSet ListDataCommand = ActivityBLL.LoadActivity(Sequence, (int?)null, null, null);
            if (ListDataCommand.Tables.Count > 0)
            {
                DataTable table = ListDataCommand.Tables[0];
                if (table.Rows.Count > 0)
                {
                    lbActDescr.Text = table.Rows[0]["bgm_proj_oper_name"].ToString();
                    lbLocation.Text = table.Rows[0]["location_text"].ToString();
                    lbcity.Text = table.Rows[0]["city_name"].ToString();
                    lbCountry.Text = table.Rows[0]["country_name"].ToString();
                    lbSchedule.Text = table.Rows[0]["job_site_name"].ToString();
                    lbDateSignStard.Text = string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_sign_start"]);
                    lbDateSignEnd.Text = string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_sign_end"]);
                    lbDateActStard.Text = string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_start"]);
                    lbDateActEnd.Text = string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_end"]);
                    lblAppDateStart.Text = string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_app_start"]);
                    lblAppDateEnd.Text = string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_app_end"]);
                    lbActLimit.Text = table.Rows[0]["act_limit"].ToString();
                    lbRemark.Text = table.Rows[0]["remarks"].ToString();
                    lblAttendCount.Text = table.Rows[0]["attend_count"].ToString();
                    lblFilterAppDateStart.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_app_start"]);

                    //lbCandidate.Text = table.Rows[0]["Candidate"].ToString();
                }
            }
        }

        //======================================== แจ้งเวียน  ====================================================  
        private void GetMessage()
        {
            int Sequence = Int32.Parse(Request.QueryString["Sequence"].ToString());
            DataSet ListDataCommand = ActivityBLL.LoadActivity(Sequence, (int?)null, (DateTime?)null, (DateTime?)null);
            DataTable table = ListDataCommand.Tables[0];

            string strActivity = "\t" + table.Rows[0]["job_site_name"].ToString() + " จัด" + table.Rows[0]["bgm_proj_oper_name"].ToString() + " ระหว่างวันที่ "
                     + (table.Rows[0]["date_activity_start"] != null ? string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_start"]) : "")
                     + (table.Rows[0]["date_activity_end"] != null ? " ถึงวันที่ " + string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_end"]) : "")
                     + " ณ " + table.Rows[0]["location_text"].ToString() + "\r\n\n\tจึงเรียนมาเพื่อทราบ"
                     + (table.Rows[0]["date_sign_start"] != null ? " หากท่านประสงค์จะสมัครเข้าร่วมกิจกรรมดังกล่าว สามารถยื่นใบสมัครได้ตั้งแต่วันที่ " + string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_sign_start"]) : "")
                     + (table.Rows[0]["date_sign_end"] != null ? " ถึงวันที่ " + string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_sign_end"]) : "")
                     + (table.Rows[0]["time_sign_end"] != null ? " ภายในเวลา  " + string.Format("{0:d MMMM yyyy}", table.Rows[0]["time_sign_end"]) + " น." : "");
            txtSendActivity.Text = strActivity;
        }
        private void GetMessageApproved()
        {
            int Sequence = Int32.Parse(Request.QueryString["Sequence"].ToString());
            DataSet ListDataCommand = ActivityBLL.LoadActivity(Sequence, (int?)null, null, null);
            DataTable table = ListDataCommand.Tables[0];

            string strApprove = "\t ตามที่ ท่านได้สมัครเข้าร่วม " + table.Rows[0]["bgm_proj_oper_name"].ToString() + " ระหว่างวันที่ "
            + (table.Rows[0]["date_activity_start"] != null ? string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_start"]) : "")
                     + (table.Rows[0]["date_activity_end"] != null ? " ถึงวันที่ " + string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_end"]) : "")
                     + " ณ " + table.Rows[0]["location_text"].ToString() + " นั้น"
                     + "\r\n\tทาง" + table.Rows[0]["job_site_name"].ToString() + "ได้พิจารณาให้ท่านเข้าร่วมกิจกรรม ดังกล่าว เป็นที่เรียบร้อยแล้ว"
                     + "\r\n\n\tจึงเรียนมาเพื่อทราบ";
            txtMessageApproved.Text = strApprove;
        }
        private void GetMessageUpper()
        {
            int Sequence = Int32.Parse(Request.QueryString["Sequence"].ToString());
            DataSet ListDataCommand = ActivityBLL.LoadActivity(Sequence, (int?)null, null, null);
            DataTable table = ListDataCommand.Tables[0];

            string strMessage = "เรียน คณะกรรมการคัดเลือกเจ้าหน้าที่เดินทางไปปฏิบัติราชการชั่วคราวในต่างประเทศ"
                + "\r\n\tตามที่ทางสำนักกิจกรรมส่งเสริมการส่งออกได้พิจารณารายชื่อผู้มีคุณสมบัติครบตาม"
                    + "หลักเกณฑ์การคัดเลือกเจ้าหน้าที่เดินทางไปปฏิบัติราชการชั่วคราวในต่างประเทศ"
                + "ประกอบด้วยเจ้าหน้าที่จำนวน " + "" + " รายดังนี้"
                //+ "\t ตามที่ ท่านได้สมัครเข้าร่วม " + table.Rows[0]["bgm_proj_oper_name"].ToString() 
                //+ " ระหว่างวันที่ " + (table.Rows[0]["date_activity_start"] != null ? string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_start"]) : "")
                //     + (table.Rows[0]["date_activity_end"] != null ? " ถึงวันที่ " + string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_end"]) : "")
                //     + " ณ " + table.Rows[0]["location_text"].ToString() + " นั้น"
                //     + "\r\n\tทาง" + table.Rows[0]["job_site_name"].ToString() + "ได้พิจารณาให้ท่านเข้าร่วมกิจกรรม ดังกล่าว เป็นที่เรียบร้อยแล้ว"
                     + "\r\n\n\tจึงเรียนมาเพื่อทราบ";
            txtMessageToUpper.Text = strMessage;
        }
        private void SendMessage()
        {
            for (int i = 0; i < RadGrid3.Items.Count; i++)
            {
                GridDataItem item = RadGrid3.Items[i];
                if (RadGrid3.Items[i].Selected)
                {
                    mesBLL.Send(ssMng.EmpSeqI
                    , Int32.Parse(item["emp_seq"].Text)
                    , DateTime.Today
                    , txtSendActivity.Text.TrimEnd()
                    , 1
                    , ssMng.EmpSeqI);
                }
            }
        }
        private void SendMessageApproved()
        {
            DataSet ds = ActReqBLL.GetActRequest(Request.QueryString["Sequence"], "1");
            DataTable dt = ds.Tables[0];

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    mesBLL.Send(ssMng.EmpSeqI
                        , Int32.Parse(dt.Rows[i]["emp_seq"].ToString())
                        , DateTime.Today
                        , txtMessageApproved.Text.TrimEnd()
                        , 1
                        , ssMng.EmpSeqI);
                }
            }
        }
        private void SendMessageUpper()
        {
            for (int i = 0; i < grdUpper.Items.Count; i++)
            {
                GridDataItem item = grdUpper.Items[i];
                if (grdUpper.Items[i].Selected)
                {
                    //ActReqBLL.InsertActRequest(Request.QueryString["Sequence"], item["emp_seq"].Text, "0", "", ssMng.EmpSeq);
                    mesBLL.Send(ssMng.EmpSeqI
                        , Int32.Parse(item["upper_emp_seq"].Text)
                        , DateTime.Today
                        , txtMessageToUpper.Text.TrimEnd()
                        , 1
                        , ssMng.EmpSeqI);
                }
            }

            //DataSet ds = ActReqBLL.GetActRequestUpper(Int32.Parse(Request.QueryString["Sequence"]), 1);
            //DataTable dt = ds.Tables[0];

            //if (dt.Rows.Count > 0)
            //{
            //    for (int i = 0; i < dt.Rows.Count; i++)
            //    {
            //        mesBLL.Send(ssMng.EmpSeq
            //            , Int32.Parse(dt.Rows[i]["emp_seq"].ToString())
            //            , DateTime.Today
            //            , txtMessageToUpper.Text.TrimEnd()
            //            , 1
            //            , ssMng.EmpSeq);
            //    }
            //}
        }
        private void SelectGridResult()
        {
            for (int i = 0; i < GridResult.Items.Count; i++)
            {
                GridDataItem item = GridResult.Items[i];
                if (GridResult.Items[i].Selected)
                {
                    ActReqBLL.InsertActRequest(Request.QueryString["Sequence"], item["emp_seq"].Text, "0", "", ssMng.EmpSeq);
                
                }
            }
        }
        private void LoadDataGrid3()
        {
            DataSet ListDataCommand = EmpBLL.LoadEmployees(null, Session["EMPTYPE"].ToString(), true, "");

            RadGrid3.DataSource = ListDataCommand;
            RadGrid3.DataBind();
        }
        protected void RadGrid3_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                UpdateUI2("AddNew");
            }
            else if (e.CommandName == "DataEdit")
            {
                UpdateUI2("Update");
            }
            else if (e.CommandName == "CircularActivituy")
            {
                GetMessage();
                Panel3.Visible = false;
                //Panel2.Visible = true;
            }
            LoadDataGrid3();
        }
        protected void LoadDataGrid4()
        {
            DataSet ds = jBLL.LoadJobSiteSearchText(SystemInfo._JobsiteInternal, "3", "");
            RadGrid4.DataSource = ds;
            RadGrid4.DataBind();
        }
        protected void RadGrid4_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                UpdateUI2("AddNew");
            }
            else if (e.CommandName == "DataEdit")
            {
                UpdateUI2("Update");
            }
            else if (e.CommandName == "CircularAll")
            {
                GetMessage();
                Panel3.Visible = false;
                //Panel2.Visible = true;
            }
            LoadDataGrid4();
        }
        private void LoadDataGridResult()
        {
            DataSet ListDataCommand = EmpBLL.LoadEmployeeForWorkingOversea(null, cmbEmployeeTypeToolTip.SelectedValue, null, Int32.Parse(cmbMonth.SelectedValue), DateTime.Parse(lblFilterAppDateStart.Text), "");

            GridResult.DataSource = ListDataCommand;
            GridResult.DataBind();
        }
        protected void GridResult_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                if (e.Item.Cells[6].Text != "0")
                {
                    //LinkButton button = e.Item.FindControl("targetControl") as LinkButton;
                    //button.ForeColor = System.Drawing.Color.Red;
                    e.Item.Cells[2].ForeColor = System.Drawing.Color.Red;
                }

                //e.Item.Cells[9].ForeColor = System.Drawing.Color.Red;// Drawing.Color.Red;

            }
        }
        protected void cmbEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["EMPTYPE"] = cmbEmployeeType.SelectedValue;
            LoadDataGrid3();
        }
        protected void cmbEmployeeTypeToolTip_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["EMPTYPE"] = cmbEmployeeTypeToolTip.SelectedValue;
            //LoadDataGrid3();
        }
        protected void rblInstitution_SelectedIndexChanged(object sender, EventArgs e)
        {
            RadGrid3.Visible = rblInstitution.SelectedValue == "1";
            RadGrid4.Visible = rblInstitution.SelectedValue == "2";
        }
        protected void btnSendTraining_Click(object sender, EventArgs e)
        {
            SendMessage();
            rblInstitution.SelectedValue = "1";
            rblInstitution_SelectedIndexChanged(sender, e);
        }

        //======================================== รายชื่อผู้สมัคร  =================================================  
        //private void LoadDataGrid2()
        //{
        //    DataSet ListDataCommand = ActReqBLL.GetActRequest(Request.QueryString["Sequence"], "");
        //    //DataTable table = ListDataCommand.Tables[0];
        //    RadGrid2.DataSource = ListDataCommand;// ListDataCommand.Tables[0];
        //    RadGrid2.DataBind();
        //}

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            //if (e.CommandName == "Delete")
            //{
            //    string seq = RadGrid2.Items[e.Item.ItemIndex]["act_request_seq"].Text;
            //    ActReqBLL.DeleteGranted(seq, "2", ssMng.EmpSeq);
            //}
            //else if (e.CommandName == "AddData")
            //{
            //    UpdateUI2("AddNew");
            //}
            //LoadDataGrid2();
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item is GridDataItem)
            //{
            //    GridDataItem item = (GridDataItem)e.Item;
            //    string strGranted = item["granted"].Text;

            //    item["ClientSelectColumn"].Enabled = strGranted != "1";
            //}
        }
        protected void btnAddGranted_OnClick(object sender, EventArgs e)
        {
            //for (int i = 0; i < RadGrid2.Items.Count; i++)
            //{
            //    GridDataItem item = RadGrid2.Items[i];
            //    if (RadGrid2.Items[i].Selected)
            //    {
            //        ActReqBLL.InsertGranted(item["act_request_seq"].Text, "1", ssMng.EmpSeq);
            //    }
            //}
            //LoadDataGrid1();

        }
        protected void btnSave2_Click(object sender, EventArgs e)
        {
            //ActReqBLL.InsertActRequest(Request.QueryString["Sequence"], cmbEmployee.SelectedValue, "0", "", ssMng.EmpSeq);
            //cmbEmployee.SelectedIndex = 0;
            LoadListBoxRequest();
            //UpdateUI2("Save");
        }
        
        protected void btnCancel2_Click(object sender, EventArgs e)
        {
            UpdateUI2("Cancel");
        }

        protected bool checkValidate()
        {
            bool bOK = true;
            //string strJobSite = cbJobSite.SelectedValue;
            //string strQuota = txtquota.Text;

            //if (strJobSite.Length == 0)
            //{
            //    bOK = false;
            //    lblError2.Text = "* กรุณาเลือกหน่วยงาน";
            //}
            return bOK;
        }
        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid2.Visible = true;
                    break;

                case "AddNew":
                    PanelGrid2.Visible = false;
                    btnSave2.Visible = true;
                    break;

                case "Cancel":
                    PanelGrid2.Visible = true;
                    break;

                case "Update":
                    PanelGrid2.Visible = false;
                    btnSave2.Visible = false;
                    break;
            }
        }

        //======================================== รายชื่อผู้มีสิทธิ์  =================================================
        private void LoadDataGrid1()
        {
            //DataSet ListDataCommand = ActReqBLL.GetActRequest(Request.QueryString["Sequence"], "1");
            //DataTable table = ListDataCommand.Tables[0]; ;
            //RadGrid1.DataSource = ListDataCommand.Tables[0];
            //RadGrid1.DataBind();
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                //string seq = RadGrid2.Items[e.Item.ItemIndex]["act_request_seq"].Text;
                //ActReqBLL.DeleteGranted(seq, "0", ssMng.EmpSeq);
            }
            else if (e.CommandName == "Approve")
            {
                int Sequence = Int32.Parse(Request.QueryString["Sequence"].ToString());
                DataSet ListDataCommand = ActivityBLL.LoadActivity(Sequence, (int?)null, null, null);
                //DataSet ListDataCommand = ActivityBLL.GetDataActivity(Request.QueryString["Sequence"].ToString(), "", "");
                DataTable table = ListDataCommand.Tables[0];

                string strApprove = "\t ตามที่ ท่านได้สมัครเข้าร่วม " + table.Rows[0]["bgm_proj_oper_name"].ToString() + " ระหว่างวันที่ "
                + (table.Rows[0]["date_activity_start"] != null ? string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_start"]) : "")
                         + (table.Rows[0]["date_activity_end"] != null ? " ถึงวันที่ " + string.Format("{0:d MMMM yyyy}", table.Rows[0]["date_activity_end"]) : "")
                         + " ณ " + table.Rows[0]["location_text"].ToString() + " นั้น"
                         + "\r\n\t" + table.Rows[0]["job_site_name"].ToString() + "ได้พิจารณาให้ท่านเข้าร่วมกิจกรรม ดังกล่าว เป็นที่เรียบร้อยแล้ว"
                         + "\r\n\n\tจึงเรียนมาเพื่อทราบ";
                txtSendQuta.Text = strApprove;
                Panel8.Visible = true;
                //Panel1.Visible = false;
            }
            else if (e.CommandName == "Disapprove")
            {
                int Sequence = Int32.Parse(Request.QueryString["Sequence"].ToString());
                DataSet ListDataCommand = ActivityBLL.LoadActivity(Sequence, (int?)null, null, null);
                //DataSet ListDataCommand = ActivityBLL.GetDataActivity(Request.QueryString["Sequence"].ToString(), "", "");
                DataTable table = ListDataCommand.Tables[0];

                string strDisapprove = "\t ตามที่ ท่านได้สมัครเข้าร่วม " + table.Rows[0]["bgm_proj_oper_name"].ToString() + " ระหว่างวันที่ "
                         + (table.Rows[0]["date_activity_start"] != null ? string.Format("{0:dd MMMM yyyy}", table.Rows[0]["date_activity_start"]) : "")
                         + (table.Rows[0]["date_activity_end"] != null ? " ถึงวันที่ " + string.Format("{0:dd MMMM yyyy}", table.Rows[0]["date_activity_end"]) : "")
                         + " ณ " + table.Rows[0]["location_text"].ToString() + " นั้น"
                         + "\r\n\t" + table.Rows[0]["job_site_name"].ToString() + "ไม่สามารถพิจารณาให้ท่านเข้าร่วมกิจกรรม ดังกล่าวได้ เนื่องจาก..."
                         + "\r\n\n\tจึงเรียนมาเพื่อทราบ";

                txtSendQuta.Text = strDisapprove;
                Panel8.Visible = true;
                //Panel1.Visible = false;
            }
            //LoadDataGrid2();
            LoadDataGrid1();
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }
        protected void btnSave1_Click(object sender, EventArgs e)
        {
            //if (checkValidate() == true)
            //{
            //    string TrainSeq = Request.QueryString["Sequence"].ToString();
            //    string User = ssMng.EmpSeq;
            //    string strComment = "";
            //    //rblTrainResult.SelectedValue();
            //    //txtTrainHour.Text;
            //    CDesBLL.InsertQuta("", TrainSeq, cbTrainee.SelectedValue, strComment, Convert.ToInt32(rblTrainResult.SelectedValue), txtTrainHour.Text, User, "1");
            //    LoadDataGrid1();
            //    UpdateUI1("Save");
            //}
        }
        protected void btnUpdate1_Click(object sender, EventArgs e)
        {
            //LoadDataGrid1();
            //UpdateUI1("Save");
        }
        protected void btnCancel1_Click(object sender, EventArgs e)
        {
            //UpdateUI1("Cancel");
            //ClearControl1();
        }
        protected void ClearControl1()
        {
        }
        protected bool checkValidate1()
        {
            bool bOK = true;

            return bOK;
        }
        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    //PanelGrid1.Visible = true;
                    break;

                case "AddNew":
                    //PanelGrid1.Visible = false;
                    //PanelAddData1.Visible = true;
                    //btnUpdate1.Visible = false;
                    //btnSave1.Visible = true;
                    break;

                case "Cancel":
                    //PanelGrid1.Visible = true;
                    //PanelAddData1.Visible = false;
                    break;

                case "Update":
                    //PanelGrid1.Visible = false;
                    //PanelAddData1.Visible = true;
                    //btnSave1.Visible = false;
                    //btnUpdate1.Visible = true;
                    break;
            }
        }

        protected void btnSendQuta_Click(object sender, EventArgs e)
        {
            Panel8.Visible = false;
            //Panel1.Visible = true;
        }

        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
            if (RadTabStrip1.SelectedIndex == 0)
            {
                LoadDetail();
            }
            else if (RadTabStrip1.SelectedIndex == 1)
            {
                //LoadDataGrid3();
                LoadDataGrid4();
            }
            else if (RadTabStrip1.SelectedIndex == 2)
            {
                LoadListBoxRequest();
                LoadListBoxApproved();
                
            }
            else if (RadTabStrip1.SelectedIndex == 3)
            {
                LoadDataGrid1();                
            }
        }
        protected void cmbEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            //cmbEmployee.Items.Clear();

            //DataSet ds = EmpBLL.LoadEmployees((int?)null, "1", true, e.Text);
            //DataTable dt = ds.Tables[0];

            //int itemOffset = e.NumberOfItems;
            //int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            //e.EndOfItems = (endOffset == dt.Rows.Count);
            //int j = 0;
            //for (int i = itemOffset; i < endOffset; i++)
            //{
            //    cmbEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["FULL_NAME"].ToString(), dt.Rows[i]["EMP_SEQ"].ToString()));

            //    TableRow tr = new TableRow();

            //    TableCell tdPosName = new TableCell();
            //    Label lblPosName = new Label();
            //    lblPosName.Text = dt.Rows[i]["FULL_NAME"].ToString();
            //    tdPosName.Width = Unit.Pixel(150);
            //    tdPosName.Controls.Add(lblPosName);
            //    tr.Cells.Add(tdPosName);

            //    TableCell tdClassSpecName = new TableCell();
            //    Label lblClassSpecName = new Label();
            //    lblClassSpecName.Text = dt.Rows[i]["POS_NAME"].ToString();
            //    tdClassSpecName.Width = Unit.Pixel(100);
            //    tdClassSpecName.Controls.Add(lblClassSpecName);
            //    tr.Cells.Add(tdClassSpecName);

            //    TableCell tdJobName = new TableCell();
            //    Label lblJobName = new Label();
            //    lblJobName.Text = dt.Rows[i]["CLASS_NAME"].ToString();
            //    tdJobName.Width = Unit.Pixel(100);
            //    tdJobName.Controls.Add(lblJobName);
            //    tr.Cells.Add(tdJobName);

            //    ((Table)cmbEmployee.Items[j].FindControl("tableEmployee")).Controls.Add(tr);
            //    j++;
            //}
            //e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }
        //************************************************************
        private void LoadDataGridUpper()
        {
            DataSet ListDataCommand = ActReqBLL.GetActRequestUpper(Int32.Parse(Request.QueryString["Sequence"]), 1);

            grdUpper.DataSource = ListDataCommand;
            grdUpper.DataBind();
        }
        private void LoadListBoxApproved()
        {
            DataSet ListDataCommand = ActReqBLL.GetActRequest(Request.QueryString["Sequence"], "1");
            DataTable info = ListDataCommand.Tables[0];

            lstApp.DataValueField = "act_request_seq";
            lstApp.DataTextField = "emp_name";
            lstApp.DataSource = info;
            lstApp.DataBind();
        }

        private void LoadListBoxRequest()
        {
            DataSet ListDataCommand = ActReqBLL.GetActRequest(Request.QueryString["Sequence"], "0");
            DataTable info = ListDataCommand.Tables[0];// pjHeaderBLL.ListRequest(cmbYear.SelectedValue, ddlStrategy.SelectedValue, ddlJobSite.SelectedValue, bgm_prj_header_seq);

            lstReq.DataValueField = "act_request_seq";
            lstReq.DataTextField = "emp_name";
            lstReq.DataSource = info;
            lstReq.DataBind();
            //checkItems();
        }
        protected void checkItems()
        {
            for (int r = lstReq.Items.Count - 1; r >= 0; r--)
            {
                for (int a = lstApp.Items.Count - 1; a >= 0; a--)
                {
                    if (lstReq.Items[r].Text == lstApp.Items[a].Text)
                    {
                        lstReq.Items.Remove(lstReq.Items[r]);
                    }
                    continue;
                }
            }
        }

        protected void btnSelect_Cleck(object sender, EventArgs e)
        {
            for (int i = lstReq.Items.Count - 1; i >= 0; i--)
            {
                if (lstReq.Items[i].Selected)
                {
                    string seq = lstReq.Items[i].Value;
                    ActReqBLL.InsertGranted(seq, "1", ssMng.EmpSeq);

                    lstApp.Items.Add(new ListItem(lstReq.Items[i].Text, lstReq.Items[i].Value));
                    lstReq.Items.Remove(lstReq.Items[i]);
                }
            }
        }

        protected void btnSelectAll_Cleck(object sender, EventArgs e)
        {
            for (int i = lstReq.Items.Count - 1; i >= 0; i--)
            {
                lstApp.Items.Add(new ListItem(lstReq.Items[i].Text, lstReq.Items[i].Value));
                lstReq.Items.Remove(lstReq.Items[i]);
            }
        }

        protected void btnRemove_Cleck(object sender, EventArgs e)
        {
            for (int i = lstApp.Items.Count - 1; i >= 0; i--)
            {
                if (lstApp.Items[i].Selected)
                {
                    string seq = lstApp.Items[i].Value;
                    ActReqBLL.DeleteGranted(seq, "0", ssMng.EmpSeq);

                    lstReq.Items.Add(new ListItem(lstApp.Items[i].Text, lstApp.Items[i].Value));
                    lstApp.Items.Remove(lstApp.Items[i]);
                }
            }
        }

        protected void btnRemoveAll_Cleck(object sender, EventArgs e)
        {
            for (int i = lstApp.Items.Count - 1; i >= 0; i--)
            {
                lstReq.Items.Add(new ListItem(lstApp.Items[i].Text, lstApp.Items[i].Value));
                lstApp.Items.Remove(lstApp.Items[i]);
            }
        }        
        protected void cmdSendApproved_Click(object sender, EventArgs e)
        {
            SendMessageApproved();
            RadAjaxManager1.Alert("ระบบได้ทำการส่งข้อความเรียบร้อยแล้ว");
        }
        protected void cmdSendUpper_Click(object sender, EventArgs e)
        {
            SendMessageUpper();
            RadAjaxManager1.Alert("ระบบได้ทำการส่งข้อความเรียบร้อยแล้ว");
        }
        protected void btnAddNewReq_Click(object sender, EventArgs e)
        {
            PanelSearchResult.Visible = true;
        }
        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            LoadDataGridResult();
        }
        protected void cmbClosePanel_Click(object sender, EventArgs e)
        {
            PanelSearchResult.Visible = false;
            LoadListBoxRequest();
        }
        protected void GridResult_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "SelectResult")
            {
                SelectGridResult();
                RadAjaxManager1.Alert("ระบบได้ทำการเพิ่มรายชื่อผู้มีคุณสมบัติครบเรียบร้อยแล้ว");
                PanelSearchResult.Visible = false;
                LoadListBoxRequest();
            }
        }

        protected void lstApp_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnMailToUpper_Click(object sender, EventArgs e)
        {
            LoadDataGridUpper();
            GetMessageUpper();
        }
        
    }
}
